package com.hpin.fansea.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hpin.fansea.R;
import com.hpin.fansea.bean.SurroundBussniseList;
import com.hpin.fansea.service.SurroundDetailActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import java.util.List;

/**
 * Created by Aaron on 15/9/11.
 */
public class SurroundListAdapter extends BaseAdapter {

    private Context context;
    private List<SurroundBussniseList> surroundBussniseLists;
    public DisplayImageOptions options;

    public SurroundListAdapter(Context context, List<SurroundBussniseList> surroundBussniseLists) {
        this.context = context;
        this.surroundBussniseLists = surroundBussniseLists;

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.icon_loading_default_list)
                .showImageForEmptyUri(R.mipmap.icon_fail_default_list)
                .showImageOnFail(R.mipmap.icon_fail_default_list)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();

    }

    public void setData(List<SurroundBussniseList> surroundBussniseLists) {
        this.surroundBussniseLists = surroundBussniseLists;
    }

    @Override
    public int getCount() {
        return surroundBussniseLists.size();
    }

    @Override
    public Object getItem(int position) {
        return surroundBussniseLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_surround_food, null);
            viewHolder = new ViewHolder();
            viewHolder.item_food_img = (ImageView) convertView.findViewById(R.id.item_food_img);
            viewHolder.item_food_title = (TextView) convertView.findViewById(R.id.item_food_title);
            viewHolder.item_food_address = (TextView) convertView.findViewById(R.id.item_food_address);
            viewHolder.item_food_comment = (TextView) convertView.findViewById(R.id.item_food_comment);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        // !TextUtils.isEmpty(surroundBussniseLists.get(position).getBusName())
        // 商家名字
//        if (!TextUtils.isEmpty(surroundBussniseLists.get(position).getBusName())) {
            viewHolder.item_food_title.setText(surroundBussniseLists.get(position).busName);
//        }
        // 商家地址
//        if (!TextUtils.isEmpty(surroundBussniseLists.get(position).addr())) {
            viewHolder.item_food_address.setText(surroundBussniseLists.get(position).addr);
//        } else {
//            viewHolder.item_food_address.setText("");
//        }
        // 商家评论
//        if (!TextUtils.isEmpty(surroundBussniseLists.get(position).getCommentOfNB())) {
            viewHolder.item_food_comment.setText(surroundBussniseLists.get(position).commentOfNB);
//        } else {
//            viewHolder.item_food_comment.setText("");
//        }

        // 图片
        ImageLoader.getInstance().displayImage(surroundBussniseLists.get(position).fmpica, viewHolder.item_food_img, options);

        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context, SurroundDetailActivity.class);
                Bundle bundle = new Bundle();
                SurroundBussniseList surroundBussniseList = surroundBussniseLists.get(position);
                bundle.putSerializable("surroundDetail", surroundBussniseList);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    private class ViewHolder {
        public ImageView item_food_img;
        public TextView item_food_title;
        public TextView item_food_comment;
        public TextView item_food_address;
    }
}